package com.zxx.casserver.modules.oplog.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxx.cascommon.base.exception.ParamsException;
import com.zxx.cascommon.base.result.Result;
import com.zxx.cascommon.base.result.ResultUtils;
import com.zxx.casserver.modules.oplog.entity.OpLogDO;
import com.zxx.casserver.modules.oplog.entity.OpLogVO;
import com.zxx.casserver.modules.oplog.service.IOpLogService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统操作日志表 前端控制器
 * </p>
 *
 * @author zhoukai
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/opLog")
public class OpLogController {

    private final IOpLogService opLogService;

    public OpLogController(IOpLogService opLogService) {
        this.opLogService = opLogService;
    }

    @PostMapping("/list")
    public Result listApp(@RequestBody OpLogVO opLogVO) {
        IPage<OpLogDO> userIPage = opLogService.listOpLog(opLogVO);
        if (ObjectUtils.isEmpty(userIPage)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(userIPage);
    }

    @PostMapping("/save")
    public Result saveApp(@RequestBody OpLogVO opLogVO) {
        if (ObjectUtils.isEmpty(opLogVO.getAction())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(opLogVO.getModules())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(opLogVO.getMsg())) {
            throw new ParamsException();
        }
        OpLogDO opLogDO = opLogService.saveOpLog(opLogVO);
        if (ObjectUtils.isEmpty(opLogDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(opLogDO);
    }


}

